package cn.chengpengper.unit19.server.handler;

import cn.chengpengper.unit19.protocol.Packet;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

import java.util.HashMap;
import java.util.Map;
import static cn.chengpengper.unit19.protocol.command.Command.*;

/**
 * @author huchengpeng
 */
public class ImRequestHandler extends SimpleChannelInboundHandler<Packet> {

    public static final ImRequestHandler INSTANCE = new ImRequestHandler();

    private Map<Byte,SimpleChannelInboundHandler<? extends Packet>> handlerMap;

    protected ImRequestHandler(){
        handlerMap = new HashMap<>(16);

        handlerMap.put(MESSAGE_REQUEST, MessageRequestPacketHandler.INSTANCE);
        handlerMap.put(CREATE_GROUP_REQUEST, CreateGroupRequestHandler.INSTANCE);
        handlerMap.put(JOIN_GROUP_REQUEST, JoinGroupRequestHandler.INSTANCE);
        handlerMap.put(QUIT_GROUP_REQUEST, QuitGroupRequestHandler.INSTANCE);
        handlerMap.put(LIST_GROUP_MEMBERS_REQUEST, ListGroupMembersRequestHandler.INSTANCE);
        handlerMap.put(GROUP_MESSAGE_REQUEST, GroupMessageRequestHandler.INSTANCE);
    }

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, Packet packet) throws Exception {
        handlerMap.get(packet.getCommand()).channelRead(ctx,packet);
    }
}
